﻿using System;
using ZP.Data;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Kwan.Data;
using System.Diagnostics;
using ZP.Service;
using System.Collections.Specialized;
using Kwan.Data.Mappings;

namespace ZP.Web
{
    public partial class Default : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Stopwatch sw = new Stopwatch(); sw.Start();
                InitHotCompanys();
                InitNewJob();
                InitArt();
                InitAD();
                sw.Stop();
                Response.Write(sw.ElapsedMilliseconds);
            }
        }

        private void InitHotCompanys()
        {
            int rndNum = new Random().Next(0, 1000);
            int pageCount = 26;
            int pageIndex = (rndNum / pageCount);
            IList<TCompany> comList = (TCompany.scale == "1").List<TCompany>(new Region(pageIndex, pageCount));
            StringBuilder sb = new StringBuilder();
            int k = 1;
            sb.Append("<ul>");
            foreach (TCompany com in comList)
            {
                if (k == (comList.Count / 2) + 1)
                {
                    sb.Append("</ul><ul>");
                }
                sb.Append("<li>" + CreateA("font_show", "/Company/" + com.EntityID + "_10_1.html", com.CompanyName) + CompanyJob(com.EntityID) + "</li>");
                k++;
            }
            sb.Append("</ul>");
            ltCom.Text = sb.ToString();
        }

        private string CompanyJob(int CompanyID)
        {
            IList<TJob> joblist = (TJob.scale == "1" & TJob.companyID == CompanyID).List<TJob>(new Region(0, 5));
            StringBuilder sb = new StringBuilder();
            foreach (TJob job in joblist)
            {
                sb.Append("&nbsp;" + CreateA("font_show_blues", "/job/" + job.EntityID + ".html", job.JobName));
            }
            return sb.ToString();
        }

        protected void InitNewJob()
        {
            IList<TJob> joblist = (TJob.scale == "1").List<TJob>(new Region(0, 40));

            StringBuilder sb = new StringBuilder();
            int k = 1;
            sb.Append("<ul>");
            foreach (TJob job in joblist)
            {
                sb.Append("<li>" + CreateA("font_show_blues", "/job/" + job.EntityID + ".html", job.JobName) + "</li>\r\n");
                if (k % 10 == 0 && k < 40)
                {
                    sb.Append("</ul><ul>");
                }
                k++;
            }
            sb.Append("</ul>");
            ltNewJob.Text = sb.ToString();
        }

        private string CreateA(string css,string href, string title)
        {
            return "<a class=\"" + css + "\" href=\"" + href + "\" title=\"" + title + "\" target=\"_blank\">" + title + "</a>";
        }

        private string CreateLi(bool isMore, string title, int id)
        {
            return isMore ? "<li class=\"artlist_more\"><a href=\"/arts/class/" + id + "_1.html\" > 更多 </a></li>" : "<li><a href=\"/arts/" + id + ".html\" title=\"" + title + "\" target=\"_blank\">" + title + "</a></li>";
        }

        public string ListToString(IList<VArticle> ArtList)
        {
            int cid = 0;
            StringBuilder sb = new StringBuilder();
            foreach (VArticle art in ArtList)
            {
                cid = art.Cid;
                sb.Append(CreateLi(false, art.Title, art.EntityID));
            }
            sb.Append(CreateLi(true, "", cid));
            return sb.ToString();
        }

        protected void InitArt()
        {
            Region r = new Region(0, 7);
            IList<VArticle> list = (VArticle.className == "职场资讯").List<VArticle>(r);
            Literal1.Text = ListToString(list);
            list = (VArticle.className == "面试技巧").List<VArticle>(r);
            Literal2.Text = ListToString(list);
            list = (VArticle.className == "培训学习").List<VArticle>(r);
            Literal3.Text = ListToString(list);
        }

        private void InitAD()
        {
            //ADMgr adMgr = new ADMgr();
            //List<TAD> adList = adMgr.GetADList(1);
            //if (adList.Count > 0)
            //{
            //    TAD ad = adMgr.GetAD(adList, "120*240");
            //    AD_120_240.Text = ad != null ? ad.Memo : "";
            //    ad = adMgr.GetAD(adList, "160*600");
            //    AD_160_600.Text = ad != null ? ad.Memo : "";
            //}
            //adMgr.Dispose();
        }

        protected int i = 1;//定义一个全局变量
        protected void HotCompanies_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //string id = DataBinder.Eval(e.Item.DataItem, "id").ToString();
            //if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            //{
            //    Repeater Jobs = (Repeater)e.Item.FindControl("Jobs");

            //    CrudMgr<TJob, VJob, int> bsjob = new CrudMgr<TJob, VJob, int>();
            //    bsjob.ViewDAO.LinqBuilder = new JobCacheQuery();
            //    NameValueCollection ps = new NameValueCollection() { { "Scale", "1" }, { "CompanyID",id } };
            //    long tbCount;
            //    List<VJob> joblist = bsjob.ViewDAO.LoadFromCache(ps, "job", 0, 4, out tbCount);
            //    bsjob.Dispose();

            //    Jobs.DataSource = joblist;
            //    Jobs.DataBind();
            //    Jobs.Dispose();
            //}

            //if (i % 2 == 0)
            //{
            //    e.Item.Controls.Add(new LiteralControl("</tr><tr>"));
            //}
            //i++;//每一条记录绑定的时候就会加1
        }
    }
}
